﻿using AnyLink.PosClient.View;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace AnyLink.PosClient.ViewModel
{
    public partial class MainViewModel : ObservableObject
    {
        readonly ILogger _logger;
        public MainViewModel(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<MainViewModel>();
            
            //FrameContent = App.Current.Services.GetRequiredService<MainPage>().Content as ContentControl;
            FrameSource= "MainPage.xaml";

            // 初始化数据
            CurrDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            UpDateCount = "0";
            VersionName = "1.0.0";

            // 创建 DispatcherTimer 实例
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1); // 设置间隔时间为1秒
            timer.Tick += new EventHandler(Time_Tick); // 注册 Tick 事件处理程序
            timer.Start(); // 开始计时器

            WeakReferenceMessenger.Default.Register<string, string>(this, "TipMessage", (r, msg) =>
            {
                MessageBox.Show(msg);
            });
        }

        //[ObservableProperty]
        //private ContentControl frameContent;

        [ObservableProperty]
        private string frameSource;

        /// <summary>
        /// 时间定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Time_Tick(object sender, EventArgs e)
        {
            CurrDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");            
        }

        [ObservableProperty]
        private string currDateTime;
        [ObservableProperty]
        private string upDateCount;
        [ObservableProperty]
        private string versionName;
        /// <summary>
        /// 退出程序
        /// </summary>
        [RelayCommand]
        private void Exit()
        {
            _logger.LogDebug("退出程序");
            Environment.Exit(0);
        }
        /// <summary>
        /// 收银
        /// </summary>
        [RelayCommand]
        private void Sale()
        {
            App.Current.Services.GetRequiredService<SaleWin>().Show();
            //App.Current.MainWindow = App.Current.Services.GetRequiredService<SaleWin>();
            //App.Current.MainWindow.Show();
            //WeakReferenceMessenger.Default.Send("关闭首页", "CloseMainWin");
            //App.Current.Services.GetRequiredService<MainWin>().Close();
        }
    }
}
